{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# check_dependencies"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/check_dependencies.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/check_dependencies.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging, sys, inspect\n",
    "from os.path import dirname, abspath\n",
    "from optparse import OptionParser\n",
    "\n",
    "\n",
    "def log_error_and_exit(error_message):\n",
    "  logging.error(error_message)\n",
    "  raise SystemExit\n",
    "\n",
    "\n",
    "#try to import setuptools\n",
    "try:\n",
    "  from setuptools import setup, Extension\n",
    "  from setuptools.command import easy_install\n",
    "except ImportError:\n",
    "  log_error_and_exit(\"\"\"setuptools is not installed for \\\"\"\"\" + sys.executable +\n",
    "                     \"\"\"\\\"\n",
    "Follow this link for installing instructions :\n",
    "https://pypi.python.org/pypi/setuptools\n",
    "make sure you use \\\"\"\"\" + sys.executable + \"\"\"\\\" during the installation\"\"\")\n",
    "\n",
    "from pkg_resources import parse_version\n",
    "\n",
    "\n",
    "def notinstalled(modulename):\n",
    "  return modulename + \"\"\" could not be imported for \\\"\"\"\" + sys.executable + \"\"\"\\\"\n",
    "Set PYTHONPATH to the output of this command \\\"make print-OR_TOOLS_PYTHONPATH\\\" before running the examples\"\"\"\n",
    "\n",
    "\n",
    "def wrong_module(module_file, modulename):\n",
    "  return \"\"\"\n",
    "The python examples are not importing the \"\"\" + modulename + \"\"\" module from the sources.\n",
    "Remove the site-package that contains \\\"\"\"\" + module_file + \"\"\"\\\", either manually or by using pip, and rerun this script again.\"\"\"\n",
    "\n",
    "\n",
    "# Returns the n_th parent of file\n",
    "def n_dirname(n, file):\n",
    "  directory = file\n",
    "  for x in range(0, n):\n",
    "    directory = dirname(directory)\n",
    "  return directory\n",
    "\n",
    "\n",
    "parser = OptionParser(\"Log level\")\n",
    "parser.add_option(\n",
    "    \"-l\",\n",
    "    \"--log\",\n",
    "    type=\"string\",\n",
    "    help=\n",
    "    \"Available levels are CRITICAL (3), ERROR (2), WARNING (1), INFO (0), DEBUG (-1)\",\n",
    "    default=\"INFO\")\n",
    "options, args = parser.parse_args()\n",
    "\n",
    "try:\n",
    "  loglevel = getattr(logging, options.log.upper())\n",
    "except AttributeError:\n",
    "  loglevel = {\n",
    "      3: logging.CRITICAL,\n",
    "      2: logging.ERROR,\n",
    "      1: logging.WARNING,\n",
    "      0: logging.INFO,\n",
    "      -1: logging.DEBUG,\n",
    "  }[int(options.log)]\n",
    "\n",
    "logging.basicConfig(\n",
    "    format=\"[%(levelname)s] %(message)s\", stream=sys.stdout, level=loglevel)\n",
    "\n",
    "logging.info(\"Python path : \" + sys.executable)\n",
    "logging.info(\"Python version : \" + sys.version)\n",
    "logging.info(\"sys.path : \" + str(sys.path))\n",
    "ortools_project_path = n_dirname(\n",
    "    3, abspath(inspect.getfile(inspect.currentframe())))\n",
    "\n",
    "#try to import ortools\n",
    "try:\n",
    "  import ortools\n",
    "except ImportError:\n",
    "  logging.error(notinstalled(\"ortools\"))\n",
    "  raise SystemExit\n",
    "\n",
    "#check if we're using ortools from the sources or it's binded by pypi's module\n",
    "ortools_module_file = inspect.getfile(ortools)\n",
    "ortools_module_path = n_dirname(3, ortools_module_file)\n",
    "if (ortools_module_path == ortools_project_path):\n",
    "  logging.info(\"Or-tools is imported from : \" + ortools_module_file)\n",
    "else:\n",
    "  log_error_and_exit(wrong_module(ortools_module_file, \"ortools\"))\n",
    "\n",
    "# Check if python can load the libraries' modules\n",
    "# this is useful when the library architecture is not compatbile with the python executable,\n",
    "# or when the library's dependencies are not available or not compatible.\n",
    "from ortools.constraint_solver import _pywrapcp\n",
    "from ortools.linear_solver import _pywraplp\n",
    "from ortools.algorithms import _pywrapknapsack_solver\n",
    "from ortools.graph import _pywrapgraph\n",
    "\n",
    "#try to import protobuf\n",
    "try:\n",
    "  import google.protobuf\n",
    "except ImportError:\n",
    "  log_error_and_exit(notinstalled(\"protobuf\"))\n",
    "\n",
    "#check if we're using protobuf from the sources or it's binded by pypi's module\n",
    "protobuf_module_file = inspect.getfile(google.protobuf)\n",
    "protobuf_module_path = n_dirname(7, protobuf_module_file)\n",
    "if (protobuf_module_path == ortools_project_path):\n",
    "  logging.info(\"Protobuf is imported from : \" + protobuf_module_file)\n",
    "else:\n",
    "  log_error_and_exit(wrong_module(protobuf_module_file, \"protobuf\"))\n",
    "\n",
    "#Check if the protobuf modules were successfully generated\n",
    "from google.protobuf import descriptor as _descriptor\n",
    "from google.protobuf import descriptor_pb2\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
